package com.group6.exam.manager.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.entity.UserInfoEntity;
import com.group6.exam.manager.service.IManagerService;
import com.group6.exam.manager.service.IUserRoleService;
import com.group6.exam.manager.service.ManagerServiceImpl;
import com.group6.exam.manager.service.UserRoleServiceImpl;

public class ManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ManagerServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");

		if ("login".equals(command)) {
			login(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("userAdd".equals(command)) {
			userAdd(request, response);
		} else if ("query".equals(command)) {
			query(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("initUpdate".equals(command)) {
			initUpdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new RuntimeException("請求失敗" + command);
		}
	}

	/**
	 * 修改用户信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String userId = request.getParameter("userId");
		String account = request.getParameter("account");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String roleId = request.getParameter("roleId");

		UserInfoEntity user = new UserInfoEntity();
		user.setId(Integer.valueOf(userId));
		user.setAccount(account);
		user.setUsername(username);
		user.setPassword(password);
		user.setRoleId(Integer.valueOf(roleId));

		IManagerService service = new ManagerServiceImpl();
		int row = service.updateUserbyId(user);
		if (row > 0) {
			response.sendRedirect("ManagerServlet?command=query");
		} else {
			response.sendRedirect("ManagerServlet?command=update");
		}
	}

	/**
	 * 修改用户前初始化用户列表
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IManagerService userService = new ManagerServiceImpl();
		IUserRoleService roleService = new UserRoleServiceImpl();
		List<Map<String, Object>> roleList = roleService.getRole();
		request.setAttribute("roleList", roleList);

		String userId = request.getParameter("userId");

		Map<String, Object> userInfo = userService.getUserInfoById(userId);
		request.setAttribute("userInfo", userInfo);

		request.getRequestDispatcher("view/manager/user/sys_updateuser.jsp")
				.forward(request, response);

	}

	/**
	 * 删除用户信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String userId = request.getParameter("userId");
		IManagerService userService = new ManagerServiceImpl();
		int row = userService.deleteUserbyId(userId);
		if (row > 0) {
			response.sendRedirect("ManagerServlet?command=query");
		} else {
			response.sendRedirect("ManagerServlet?command=query");
		}
	}

	/**
	 * 显示用户信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 分页
		PageModel pageModel = new PageModel();
		// 获取当前页
		String currentPage = request.getParameter("currentPage");
		int currentPageNo = !"".equals(currentPage) && currentPage != null ? Integer
				.valueOf(currentPage) : 1;
		pageModel.setCurrentPage(currentPageNo);

		String userId = request.getParameter("userId");
		pageModel.setUserId(userId);
		IManagerService userService = new ManagerServiceImpl();

		List<Map<String, Object>> userList = userService.getUserInfo();
		PageModel paging = userService.check(pageModel);
		request.setAttribute("userList", userList);
		request.setAttribute("paging", paging);
		request.getRequestDispatcher("view/manager/user/sys_maintenance.jsp")
				.forward(request, response);
	}

	/**
	 * 添加用户信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void userAdd(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String roleId = request.getParameter("roleId");

		UserInfoEntity user = new UserInfoEntity();
		user.setAccount(account);
		user.setUsername(username);
		user.setPassword(password);
		user.setRoleId(Integer.valueOf(roleId));

		IManagerService service = new ManagerServiceImpl();
		int row = service.addUser(user);
		if (row > 0) {
			response.sendRedirect("ManagerServlet?command=query");
		} else {
			response.sendRedirect("UserRoleServlet");
		}
	}

	/**
	 * 获取菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		IManagerService service = new ManagerServiceImpl();

		List<Map<String, Object>> mainMenus = service.getMainMenus(username);
		List<Map<String, Object>> childMenus = service.getChildMenus(username);

		request.setAttribute("mainMenus", mainMenus);
		request.setAttribute("childMenus", childMenus);
		request.getRequestDispatcher("index.jsp").forward(request, response);

	}

	/**
	 * 登录
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		PrintWriter out = response.getWriter();

		String username = request.getParameter("username");
		String password = request.getParameter("password");

		UserInfoEntity user = new UserInfoEntity();
		user.setAccount(username);
		user.setPassword(password);

		IManagerService service = new ManagerServiceImpl();

		if (service.login(user)) {
			request.getSession().setAttribute("username", username);
			response.sendRedirect("ManagerServlet?command=main&username="
					+ username);
		} else {
			out.print("<script>alert('登录失败');location.href='login.jsp';</script>");
		}

		out.flush();
		out.close();

	}

}
